package com.hj.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.hj.utils.StatusUtil;
import lombok.Data;


/**
 * 后台统一返回的 标准响应封装类
 *
 * @author haoshen
 * @version 1.0
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@SuppressWarnings({"unchecked", "rawtypes", "unused"})
public class AdvanceResponse<T> {
    /**
     * 状态码  1代表成功  0代表失败
     */
    private Integer status;
    /**
     * 要封装的主要数据
     */
    private T data;
    /**
     * 一些需要返回的基本信息
     */
    private String msg;

    private AdvanceResponse() {
    }

    private AdvanceResponse(Integer status) {
        this.status = status;
    }

    private AdvanceResponse(Integer status, T data) {
        this.status = status;
        this.data = data;
    }

    private AdvanceResponse(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    private AdvanceResponse(Integer status, T data, String msg) {
        this.status = status;
        this.data = data;
        this.msg = msg;
    }

    public static AdvanceResponse successResponse() {
        return new AdvanceResponse(StatusUtil.SUCCESS_STATUS);
    }

    public static AdvanceResponse successResponse(String msg) {
        return new AdvanceResponse(StatusUtil.SUCCESS_STATUS, msg);
    }

    public static <T> AdvanceResponse successResponse(T data) {
        return new AdvanceResponse(StatusUtil.SUCCESS_STATUS, data);
    }

    public static <T> AdvanceResponse successResponse(T data, String msg) {
        return new AdvanceResponse(StatusUtil.SUCCESS_STATUS, data, msg);
    }

    public static AdvanceResponse failedResponse(String msg) {
        return new AdvanceResponse(StatusUtil.FAILED_STATUS, msg);
    }

    /**
     * 判断当前请求是否成功
     * 不被json解析
     *
     * @return 是否成功
     */
    @JsonIgnore
    public Boolean isSuccess() {
        return status.equals(StatusUtil.SUCCESS_STATUS);
    }
}
